<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>管理员列表-WeAdmin Frame型后台管理系统-WeAdmin 1.0</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="../../css/font.css">
		<link rel="stylesheet" href="../../css/weadmin.css">
		<!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
		<!--[if lt IE 9]>
	      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
	      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
	    <![endif]-->
	</head>

	<body>
		<div class="weadmin-nav">
			<span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">管理员管理</a>
        <a>
          <cite>管理员列表</cite></a>
      </span>
			<a class="layui-btn layui-btn-sm" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
				<i class="layui-icon" style="line-height:30px">ဂ</i></a>
		</div>
		<div class="weadmin-body">
			<!--<div class="layui-row">
				<form class="layui-form layui-col-md12 we-search">
					<div class="layui-inline">
						<input class="layui-input" placeholder="开始日" name="start" id="start">
					</div>
					<div class="layui-inline">
						<input class="layui-input" placeholder="截止日" name="end" id="end">
					</div>
					<div class="layui-inline">
						<input type="text" name="username" placeholder="请输入用户名" autocomplete="off" class="layui-input">
					</div>
					<button class="layui-btn" lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
				</form>
			</div>-->
			<!--<div class="weadmin-block">
				<button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
				<button class="layui-btn" onclick="WeAdminShow('添加用户','./add.html')"><i class="layui-icon"></i>添加</button>
			</div>-->
			<table class="layui-table" id="adminList" lay-filter="adminTable">

			</table>
			<script type="text/html" id="barDemoAdmin">
				<!--<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>-->
				<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
				<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
			</script>
		</div>

		<script src="../../lib/layui/layui.js" charset="utf-8"></script>
    	<script src="../../js/eleDel.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../lib/jquery-2.1.1/jquery.js"></script>
		<script>
			layui.use(['laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element', 'slider'], function() {
				var laydate = layui.laydate //日期
						, laypage = layui.laypage //分页
						, layer = layui.layer //弹层
						, table = layui.table //表格
						, carousel = layui.carousel //轮播
						, upload = layui.upload //上传
						, element = layui.element //元素操作
						, slider = layui.slider //滑块



				//执行一个 table 实例
				table.render({
					elem: '#adminList'
					, height: 500
					,method: 'post'
					, url: '/user/queryAllByLimit' //数据接口
					, title: '管理员表'
					, page: true //开启分页
					, toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
					, totalRow: false //开启合计行
					, cols: [[ //表头
						{type: 'checkbox', fixed: 'left'}
						, {field: 'id', title: 'ID', width: 100, sort: true, fixed: 'left', totalRowText: '合计：'}
						, {field: 'username', title: '用户名', width: 200}
						, {field: 'createTime', title: '创建时间', width: 200,  totalRow: true}
						,{fixed: 'right', width: 165, align:'center', toolbar: '#barDemoAdmin'}
					]]
				});
				//监听头工具栏事件
				table.on('toolbar(adminTable)', function(obj){
					var checkStatus = table.checkStatus(obj.config.id)
							,data = checkStatus.data; //获取选中的数据
					switch(obj.event){
						case 'add':
							WeAdminShow('添加用户','./add.html',600,400)
							break;
						case 'update':
							if(data.length === 0){
								layer.msg('请选择一行');
							} else if(data.length > 1){
								layer.msg('只能同时编辑一个');
							} else {
								localStorage.setItem("userId",checkStatus.data[0].id)
								console.log(checkStatus.data[0])
								localStorage.setItem("user_name",checkStatus.data[0].username)
								WeAdminShow('修改密码','./edit.html',600,400)

							}
							break;
						case 'delete':
							if(data.length === 0){
								layer.msg('请选择一行');
							} else {
								console.log(data)
								var ids=[]
								for (var i=0;i<data.length;i++){
									ids.push(data[i].id)
								}
								console.log(ids)
								$.ajax({
									url: '/user/deleteByIds',
									method: 'post',
									data:{
										"ids":ids
									},
									success(res){
										layer.msg("删除成功")
										window.location.reload();//修改成功后刷新父界面
									},
									error(){

									}
								})
							}
							break;
					};
				});

				//监听行工具事件
				table.on('tool(adminTable)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
					var data = obj.data //获得当前行数据
							,layEvent = obj.event; //获得 lay-event 对应的值
					if(layEvent === 'detail'){
						layer.msg('查看操作');
					} else if(layEvent === 'del'){
						layer.confirm('真的删除行么', function(index){
							console.log(obj.data.id,index)
							$.ajax({
								url: '/user/delete',
								method:'post',
								data:{
									"id":obj.data.id
								},
								success(res){
									obj.del(); //删除对应行（tr）的DOM结构
									layer.close(index);
								},
								error(){

								}
							})

						});
					} else if(layEvent === 'edit'){

						localStorage.setItem("userId",data.id)
						localStorage.setItem("user_name",data.username)
						WeAdminShow('修改密码','./edit.html',600,400)
					}
				});
			});
		</script>
	</body>

</html>